import PopupBase from "../../base/Script/components/popups/PopupBase";
import BaseScene from "../../base/Script/core/BaseScene";
import BundleManager from "../../base/Script/core/BundleManager";
import EventManager from "../../base/Script/core/EventManager";
import ResManager from "../../base/Script/core/ResManager";
import LogUtil from "../../base/Script/utils/LogUtil";
import NodeUtil from "../../base/Script/utils/NodeUtil";
import { Parts,SpineParts } from "./ccCommon";
import ClothesData from "./ccClothesData";
import CameraCapture from "../../base/Script/components/CameraCapture";
import ImageUtil from "../../base/Script/utils/ImageUtil";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ccGame extends BaseScene {
    @property(CameraCapture)
    camereCapture: CameraCapture = null;
    private spineParts = {};
    onLoad () {
        super.onLoad();
        let role = NodeUtil.getChildByName(this.node,'role');
        this.spineParts[SpineParts.suti] = NodeUtil.getChildByName(role,'spine_suti',sp.Skeleton);
        this.spineParts[SpineParts.lian] = NodeUtil.getChildByName(role,'spine_lian',sp.Skeleton);
        this.spineParts[SpineParts.toufaqian] = NodeUtil.getChildByName(role,'spine_toufaqian',sp.Skeleton);
        this.spineParts[SpineParts.toufahou] = NodeUtil.getChildByName(role,'spine_toufahou',sp.Skeleton);
        this.spineParts[SpineParts.chibang] = NodeUtil.getChildByName(role,'spine_chibang',sp.Skeleton);
        this.spineParts[SpineParts.neiyi] = NodeUtil.getChildByName(role,'spine_neiyi',sp.Skeleton);
        this.spineParts[SpineParts.xiayi] = NodeUtil.getChildByName(role,'spine_xiayi',sp.Skeleton);
        this.spineParts[SpineParts.xiayihou] = NodeUtil.getChildByName(role,'spine_xiayihou',sp.Skeleton);
        this.spineParts[SpineParts.shangyi] = NodeUtil.getChildByName(role,'spine_shangyi',sp.Skeleton);
        this.spineParts[SpineParts.shangyi2] = NodeUtil.getChildByName(role,'spine_shangyi2',sp.Skeleton);
        this.spineParts[SpineParts.xiuzi] = NodeUtil.getChildByName(role,'spine_xiuzi',sp.Skeleton);
        this.spineParts[SpineParts.xiezi] = NodeUtil.getChildByName(role,'spine_xiezi',sp.Skeleton);
        this.spineParts[SpineParts.xiezihou] = NodeUtil.getChildByName(role,'spine_xiezihou',sp.Skeleton);
        
        this.AsyncGetConfig();
        ClothesData.ins().load();
        EventManager.on('updateSpine',this.updateSpine,this);
    }

    onDestroy() {
        super.onDestroy();
        EventManager.off('updateSpine',this.updateSpine,this);
        BundleManager.ins().releaseAll('ChangeClothes');
    }

    async AsyncGetConfig() {
        let obj = {
            resourceId: 'config_shaonv',
            resourcePath: 'config/shaonv',
            resourceType: 'csv',
            key: ['partId','idx']
        }
        let _config = await ResManager.ins().getResAsync(obj);
        LogUtil.log(LogUtil.getFileName(),_config);
    }

    onMenuBtnClick(event, customEventData){
        let _index = parseInt(customEventData);
        LogUtil.log('_index',_index);
        this.loadPop('ChangeClothes','prefabs/popClothes', {partId:_index});
    }

    onBtnSaveScreenshot(){
        let data = this.camereCapture.captureTexture();
        ImageUtil.onSaveTap(null, data, 420, 520);
    }

    loadPop(bundle:string, url:string, options:any){
        BundleManager.ins().loadBundle(bundle,url,(loadedResource) => {
            let node = cc.instantiate(loadedResource);
            node.setParent(cc.Canvas.instance.node);
            let ctrl:PopupBase = node.getComponent(PopupBase);
            ctrl.show(options);
        },cc.Prefab);
    }
    initSpineAnimation(){
        //可能要重新播放下所有spine动画，以防止不同步
        for(let k in this.spineParts){
            let spine = this.spineParts[k];
            if(spine.skeletonData){
                spine.setAnimation(0, 'start', true);
            }
        }
    }
    loadSpineLian(options){
        if(options.selected){
            if(options.partPath){
                let url = 'spine_dingzhishaonv/'+options.partPath;
                this.loadSpine('ChangeClothes',url,this.spineParts[SpineParts.lian]);
            }
        }else{
            // this.spineParts[SpineParts.lian].skeletonData = null;
        }
    }
    loadSpineToufa(options){
        if(options.selected){
            if(options.partPath){
                let url = 'spine_dingzhishaonv/'+options.partPath;
                this.loadSpine('ChangeClothes',url,this.spineParts[SpineParts.toufaqian]);
            }
            if(options.partPath2){
                let url2 = 'spine_dingzhishaonv/'+options.partPath2;
                this.loadSpine('ChangeClothes',url2,this.spineParts[SpineParts.toufahou]);
            }
        }else{
            // this.spineParts[SpineParts.toufaqian].skeletonData = null;
        }
    }
    loadSpineNeiyi(options){
        if(options.selected){
            if(options.partPath){
                let url = 'spine_dingzhishaonv/'+options.partPath;
                this.loadSpine('ChangeClothes',url,this.spineParts[SpineParts.neiyi]);
            }
        }else{
            this.spineParts[SpineParts.neiyi].skeletonData = null;
        }
    }
    loadSpineXiayi(options){
        if(options.selected){
            if(options.partPath){
                let url = 'spine_dingzhishaonv/'+options.partPath;
                this.loadSpine('ChangeClothes',url,this.spineParts[SpineParts.xiayi]);
            }
            if(options.partPath2){
                let url2 = 'spine_dingzhishaonv/'+options.partPath2;
                this.loadSpine('ChangeClothes',url2,this.spineParts[SpineParts.xiayihou]);
            }else{
                this.spineParts[SpineParts.xiayihou].skeletonData = null;
            }
        }else{
            this.spineParts[SpineParts.xiayi].skeletonData = null;
            this.spineParts[SpineParts.xiayihou].skeletonData = null;
        }
    }
    loadSpineShangyi(options){
        if(options.selected){
            if(options.partPath){
                let url = 'spine_dingzhishaonv/'+options.partPath;
                this.loadSpine('ChangeClothes',url,this.spineParts[SpineParts.shangyi]);
            }
            if(options.partPath2){
                let url2 = 'spine_dingzhishaonv/'+options.partPath2;
                this.loadSpine('ChangeClothes',url2,this.spineParts[SpineParts.shangyi2]);
            }else{
                this.spineParts[SpineParts.shangyi2].skeletonData = null;
            }
            if(options.partPath3){
                let url2 = 'spine_dingzhishaonv/'+options.partPath3;
                this.loadSpine('ChangeClothes',url2,this.spineParts[SpineParts.xiuzi]);
            }else{
                this.spineParts[SpineParts.xiuzi].skeletonData = null;
            }
        }else{
            this.spineParts[SpineParts.shangyi].skeletonData = null;
            this.spineParts[SpineParts.shangyi2].skeletonData = null;
            this.spineParts[SpineParts.xiuzi].skeletonData = null;
        }
    }
    loadSpineXiezi(options){
        if(options.selected){
            if(options.partPath){
                let url = 'spine_dingzhishaonv/'+options.partPath;
                this.loadSpine('ChangeClothes',url,this.spineParts[SpineParts.xiezi]);
            }
            if(options.partPath2){
                let url2 = 'spine_dingzhishaonv/'+options.partPath2;
                this.loadSpine('ChangeClothes',url2,this.spineParts[SpineParts.xiezihou]);
            }else{
                this.spineParts[SpineParts.xiezihou].skeletonData = null;
            }
        }else{
            this.spineParts[SpineParts.xiezi].skeletonData = null;
            this.spineParts[SpineParts.xiezihou].skeletonData = null;
        }
    }
    loadSpineChibang(options){
        if(options.selected){
            let url = 'spine_dingzhishaonv/'+options.partPath;
            this.loadSpine('ChangeClothes',url,this.spineParts[SpineParts.chibang]);
        }else{
            this.spineParts[SpineParts.chibang].skeletonData = null;
        }
    }

    updateSpine(options:any) {
        this.initSpineAnimation();
        switch(options.partId){
            case Parts.lian:
                this.loadSpineLian(options);
                break;
            case Parts.toufa:
                this.loadSpineToufa(options);
                break;
            case Parts.neiyi:
                this.loadSpineNeiyi(options);
                break;
            case Parts.xiayi:
                this.loadSpineXiayi(options);
                break;
            case Parts.shangyi:
                this.loadSpineShangyi(options);
                break;
            case Parts.xiezi:
                this.loadSpineXiezi(options);
                break;
            case Parts.chibang:
                this.loadSpineChibang(options);
                break;
        }
    }

    loadSpine(bundle:string, url:string, spine:sp.Skeleton){
        BundleManager.ins().loadBundle(bundle,url,(loadedResource) => {
            spine.skeletonData = loadedResource;
            spine.setAnimation(0, 'start', true);
        },sp.SkeletonData);
    }
}
